package com.shigu.activity.service;

import com.opentae.data.mall.beans.ActivityInfo;
import com.opentae.data.mall.examples.ActivityInfoExample;
import com.opentae.data.mall.interfaces.ActivityInfoMapper;
import com.shigu.PageResponse;
import com.shigu.activity.api.ActivityService;
import com.shigu.activity.vo.ActivityListVo;
import com.shigu.activity.vo.IndexActivityListVo;
import com.shigu.exceptions.AdminException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service(value = "activityServiceImpl")
public class ActivityServiceImpl implements ActivityService{

    @Autowired
    ActivityInfoMapper activityInfoMapper;

    @Override
    public PageResponse<IndexActivityListVo> getIndexActivityList() {
        List<ActivityInfo> activityInfos = activityInfoMapper.selectByExample(new ActivityInfoExample());
        PageResponse<IndexActivityListVo> response = new PageResponse<>();
        response.setData(activityInfos.stream().map(o->{
            IndexActivityListVo vo = new IndexActivityListVo();
            vo.setaId(o.getActivityId().toString());
            vo.setImgSrc(o.getImage());
            vo.setTitle(o.getActivityTitle());
            return vo;
        }).collect(Collectors.toList()));
        return response;
    }

    @Override
    public void setActivityItem(Long aId, String title, String smallImgSrc, String bgImgSrc, Integer type) throws AdminException {
        if(StringUtils.isBlank(title)){
            throw new AdminException("活动标题不能为空");
        }
        if(StringUtils.isBlank(smallImgSrc)){
            throw new AdminException("请上传活动图");
        }
        if(StringUtils.isBlank(bgImgSrc)){
            throw new AdminException("请上传背景图");
        }
        if(type == null){
            throw new AdminException("请选择活动类型");
        }
        ActivityInfo info = new ActivityInfo();
        info.setActivityTitle(title);
        info.setImage(smallImgSrc);
        info.setBackgroundImg(bgImgSrc);
        info.setType(type);
        if(aId == null){
            activityInfoMapper.insertSelective(info);
        }else{
            info.setActivityId(aId);
            activityInfoMapper.updateByPrimaryKeySelective(info);
        }
    }

    @Override
    public PageResponse<ActivityListVo> getActivityList() {
        ActivityInfoExample example = new ActivityInfoExample();
        example.setOrderByClause("create_time desc");
        List<ActivityInfo> activityInfos = activityInfoMapper.selectByExample(example);
        PageResponse<ActivityListVo> response = new PageResponse<>();
        response.setData(activityInfos.stream().map(o->{
            ActivityListVo vo = new ActivityListVo();
            vo.setaId(o.getActivityId());
            vo.setTitle(o.getActivityTitle());
            vo.setSmallImgSrc(o.getImage());
            vo.setBgImgSrc(o.getBackgroundImg());
            vo.setType(o.getType().toString());
            return vo;
        }).collect(Collectors.toList()));
        return response;
    }

    @Override
    public void deleteActivityItem(Long aId) throws AdminException {
        if(aId == null){
            throw new AdminException("参数异常");
        }
        activityInfoMapper.deleteByPrimaryKey(aId);
    }
}
